package com.xiang.mall.framework.base.protocol

import com.xiang.mall.framework.base.constant.ErrorStatus

/**
 * @author xiangliyou
 * @email xiangliyou1814@mail.yneusoft.com
 * @date 2019-03-06 11:45
 *
 */
data class ApiResult<T>(
    /**
     * 错误时的提示信息
     */
    var msg: String = "",

    /**
     * 状态是否成功
     */
    var success: Boolean = false,

    /**
     * 返回的消息类型
     */
    var code: Int = 400,

    /**
     * 返回的结构
     */
    var data: T? = null
)
{
    companion object {
        /**
         * 构建返回的结果
         * code 默认为 0
         */
        fun <T> build(success: Boolean, msg: String, code: Int = 0, data: T?): ApiResult<T> {
            return ApiResult(msg, success, code, data)
        }

        /**
         * 构建成功的结果
         */
        fun <T> buildSuccess(msg: String = "success", data: T? = null): ApiResult<T> {
            return build(success = true, msg = msg, data = data)
        }

        /**
         * 构建失败的结果
         * code 默认为0
         */
        fun <T> buildFailure(msg: String = "failure", code: Int = 0, data: T? = null): ApiResult<T> {
            return build(success = false, msg = msg, code = code, data = data)
        }

        /**
         * 使用错误码构建失败的结果
         */
        fun <T> buildFailure(status: ErrorStatus, data: T? = null): ApiResult<T> {
            return build(success = false, msg = status.message, code = status.value, data = data)
        }
    }
}